package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
//前台用户
public class UserController {
    @Autowired
    private UserService userService;

    //前台验证码
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone=user.getPhone();
        //判断是否输入验证码
        if (StringUtils.isNotEmpty(phone)){
            String code="123456";
            //通过工具类生成4位验证码
//            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            //打印验证码日志
//            log.info("code={}",code);
            //调用阿里云提供的短信服务API完成发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            //需要将生成的验证码保存到Session
            session.setAttribute(phone,code);
            //返回验证码成功
            return R.success("手机验证码短信发送成功");
        }
        //返回验证码失败
        return R.error("短信发送失败");
    }
    //前台登录
    @PostMapping("/login")
    public R<User> login(@RequestBody Map<String,String> map,HttpSession httpSession){
        //打印获得的前台手机号和验证码
        log.info(map.toString());
        //获取手机号
        String phone = map.get("phone").toString();
        //获取验证码
        String code = map.get("code").toString();
        //从Session中获取验证码
        Object attribute = httpSession.getAttribute(phone);
        //进行验证码的比对（页面提交的验证码和Session中保存的验证码比对）
        if (attribute!=null&&attribute.equals(code)){
            //创建构造器
            LambdaQueryWrapper<User> ulqw = new LambdaQueryWrapper<>();
            //创建构造条件查找出手机号
            ulqw.eq(User::getPhone,phone);
            //查询
            User user = userService.getOne(ulqw);
            //判断该手机号是否注册，没有则注册
            if (user==null){
                //创建一个空user对象
                user=new User();
                //设置手机号
                user.setPhone(phone);
                //设置状态启用
                user.setStatus(1);
                //设置默认name
                user.setName(RandomStringUtils.randomAscii(10));
                //注册
                userService.save(user);
            }
            httpSession.removeAttribute(phone);
            //吧当前用户的id储存进去
            httpSession.setAttribute("user",user.getId());
            //返回登录成功的对象
            return R.success(user);
        }

        //否则返回登录失败
        return R.error("登录失败");
    }
    //退出
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session){
        session.removeAttribute("user");
        return R.success("退出登录成功");
    }
}